﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;
using Resources;

namespace MonkeyWarranty.Web.Errors
{
    public partial class Edit : System.Web.UI.Page
    {
        #region Properties

        private ErrorBiz _errorBiz;

        private ErrorBiz ErrorManager
        {
            get { return _errorBiz ?? (_errorBiz = new ErrorBiz()); }
        }

        private int _errorId;
        private int ErrorId
        {
            get
            {
                if (_errorId <= 0)
                    int.TryParse(Request.QueryString["Id"], out _errorId);
                return _errorId;
            }
        }

        private Error _error;
        private Error CurrentError
        {
            get
            {
                if (_error == null && ErrorId > 0)
                {
                    _error = ErrorManager.GetById(ErrorId);
                }
                return _error;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Load data
                if (CurrentError == null)
                {
                    lblErrorMessage.Text = lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, ErrorId);
                    btnSave.Enabled = false;
                }
                else
                {
                    txtCode.Text = CurrentError.Code;
                    txtName.Text = CurrentError.Name;
                    txtDescription.Text = CurrentError.Description;
                    txtSortOrder.Text = CurrentError.SortOrder.ToString(CultureInfo.InvariantCulture);
                    chkHidden.Checked = CurrentError.Hidden;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;

            CurrentError.Code = code;
            CurrentError.Name = name;
            CurrentError.Description = descrition;
            if (sortOrder > 0)
                CurrentError.SortOrder = sortOrder;
            CurrentError.Hidden = isHidden;

            // update
            ErrorManager.UpdateError(CurrentError);

            lblErrorMessage.Text = GlobalStrings.UpdateSuccess;
            errorMessage.Visible = true;
        }

        #endregion
    }
}